package csu.fun.spring_store.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import csu.fun.spring_store.domain.*;
import csu.fun.spring_store.persistence.CategoryMapper;
import csu.fun.spring_store.persistence.ItemMapper;
import csu.fun.spring_store.persistence.OrderMapper;
import csu.fun.spring_store.persistence.ProductMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.*;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
@Validated
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    @RequestMapping("/OrderManagement")
    public String allorder(Model model) {

        return "OrderManagement";
    }
    @RequestMapping("/Ordertongji")
    public String ordertongji()
    {
        return "OrderTongji";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam(value = "id") String userid) {
        System.out.println(userid);
        orderMapper.deleteById(userid);
        orderMapper.deleteorderinlineitem( Integer.parseInt(userid));
        return "删除成功";
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONObject update(@RequestParam(value = "id") String userid) {
        //List<OrderVO> order = orderMapper.selectOrders();
        JSONObject m=new JSONObject();
        Order order = orderMapper.getOrderByOrderid(Integer.parseInt(userid));
        List<JSONObject> item=orderMapper.selectlineitem(Integer.parseInt(userid));
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(order);
        System.out.println(item.size());
        m.put("order",jsonObject);
        m.put("item",item);
        return m;
    }
    @RequestMapping("/getdate")
    @ResponseBody
    public JSONObject getdate(){
        JSONObject z=new JSONObject();

        return z;
    }

    @RequestMapping("/bianji")
    @ResponseBody
    public String bianji(@RequestBody List<JSONObject> m){

        Order order=new Order();
        order.setOrderid(Integer.parseInt(m.get(0).get("orderid").toString()));
        order.setUserid(m.get(0).get("userid").toString());
        //order.setOrderDate(m.get(0).get("orderdate"));
        order.setBillAddr1(m.get(0).get("billAddr1").toString());
        order.setBillToFirstname(m.get(0).get("billToFirstname").toString());
        order.setCreditCard(m.get(0).get("creditCard").toString());
        order.setCardType(m.get(0).get("cardType").toString());
        BigDecimal zss=new BigDecimal(m.get(0).get("totalPrice").toString());
        order.setTotalPrice(zss);;
        order.setShipAddr1(m.get(0).get("shipAddr1").toString());
        order.setShipToFirstname(m.get(0).get("shipToFirstname").toString());
        orderMapper.updateById(order);
        m.get(1).get("name");
        m.get(1).get("shuliang");
        orderMapper.bianjiitem( Integer.parseInt(m.get(0).get("orderid").toString()),
                m.get(1).get("name").toString(), Integer.parseInt(m.get(1).get("shuliang").toString()));

            return "yes";
    }

    @RequestMapping("/category")
    @ResponseBody
    public List<Product> queryproduct(String mm){


            QueryWrapper<Product> wrapper=new QueryWrapper();
            wrapper.eq("category",mm);
            List<Product> productList=productMapper.selectList(wrapper);
            return productList;

    }
    @RequestMapping("/tianjia")
    @ResponseBody
    public String additem(@RequestParam(value = "id") String mm ,@RequestParam(value = "orderid")int orderid){
        System.out.println(mm);
        String ds=mm.substring(1);
        Item item=itemMapper.selectById(ds);
        System.out.println(item);
        List<JSONObject> item2= orderMapper.selectlineitem(orderid);
        System.out.println(orderid);
        orderMapper.addline(orderid,item2.size(),ds,1,item.getListprice());
        return "yes";

    }
    @RequestMapping("/orderless")
    @ResponseBody
    public String orderless(@RequestParam(value = "id")String id,@RequestParam(value = "item")String itemid){
        orderMapper.deleteordernum(Integer.parseInt(id),itemid);
        return "成功";
    }
//    @RequestMapping("/fahuo")
//    @ResponseBody
//    public String orderless(@RequestParam(value = "id")String id){
//        orderMapper.deleteordernum(Integer.parseInt(id));
//        return "成功";
//    }

}
